package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.CarBean;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.ParkInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/16.
 */
public class ParkInteractorImp extends NetWorkImp implements ParkInteractor {

    RequestListener<List<CarBean>> listener;

    public ParkInteractorImp(Context c) {
        super(c);
    }

    public ParkInteractorImp(Context c, RequestListener<List<CarBean>> listener) {
        this(c);
        this.listener = listener;
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {

        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
//            onRequestExcption(null);

        } else {
            int statue = response.getInteger(MethodCode.responseStatue);

            if (statue == (MethodCode.NetState.SUCCESS.getValue())) {

                if (tag.toString().equals(MethodType.GET_CARLIST)) {
                    JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                    JSONArray parking = obj.getJSONArray("cars");

                    List<CarBean> parks = JSON.parseArray(parking.toJSONString(), CarBean.class);
                    listener.Success(tag + "", parks);
                } else
                    listener.Error(tag + "", "获取可用车辆失败，请尝试刷新");
            }else
                listener.Error(tag + "", "获取可用车辆失败，请尝试刷新");
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("", error);
    }

    @Override
    public void getRestCar(String userId, String parkId, int type, int currentPage) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("sessionId", userId);
        params.put("parkingId", parkId);
        params.put("type", type);
        bean.setRequestParams(params);
        bean.setRequestContentCurrentPage(currentPage);
        bean.setRequestCode(MethodType.GET_CARLIST);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_CARLIST, null, (JSONObject) JSON.toJSON(bean));
        startRequest();
//        onRequestSuccess("tag", new JSONObject());
    }
}
